{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 下面代码输出什么？\n",
    "print(\"You are doing well\"[2:100])\n",
    "print(\"You are doing well\"[2:4])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 执行下面代码输出什么？\n",
    "a = (1, 2)\n",
    "a[0] += 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 执行下面代码输出什么\n",
    "s = 0\n",
    "for d in range(0, 5, 0.1):\n",
    "    s += d\n",
    "    print(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 下面代码分别输出什么\n",
    "print(min(\"international\"))\n",
    "print(min(1, 2, 3))\n",
    "print(min([1, 2, 3]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 下面代码的输出结果是什么并尝试解释\n",
    "def total(initial = 5, *num, **key): \n",
    "    count = initial\n",
    "    \n",
    "    for n in num:\n",
    "        count += n\n",
    "        \n",
    "    # count = 105\n",
    "    \n",
    "    for k in key:\n",
    "        count += key[k]\n",
    "        \n",
    "    # count = 255\n",
    "        \n",
    "    return count\n",
    "\n",
    "print(total(100, 2, 3, clouds = 50, stars = 100))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 下面代码输出什么并尝试解释\n",
    "class A():\n",
    "    def __init__(self, i = 100):\n",
    "        self.i = i\n",
    "        \n",
    "class B(A):\n",
    "    def __init__(self, j = 0):\n",
    "        self.j = j\n",
    "        \n",
    "def main():\n",
    "    b = B()\n",
    "    print(b.i)\n",
    "    print(b.j)\n",
    "    \n",
    "main()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 下面代码输出什么并尝试解释\n",
    "a, b = c = 2 + 2, \"Hello, World\"\n",
    "print(a)\n",
    "print(b)\n",
    "print(c)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 下面代码输出什么\n",
    "import pandas as pd\n",
    "\n",
    "games = {'Name' : ['Call Of Duty', 'Total Overdose', 'GTA 3', 'Bully'],\n",
    "       'Play Time(in hours)' : ['45', '46', '52', '22'],\n",
    "        'Rate' : ['Better than Average', 'Good', 'Best', 'Average']}\n",
    "\n",
    "df = pd.DataFrame(games)\n",
    "df['ranking'] = df['Play Time(in hours)'].rank(ascending = 0)\n",
    "\n",
    "print(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 通过经纬度获取城市名称\n",
    "from geopy.geocoders import Nominatim\n",
    "\n",
    "geolocator = Nominatim(user_agent=\"MyApp\")\n",
    "\n",
    "coordinates = \"25.02, 102.38\"\n",
    "\n",
    "try:\n",
    "    location = geolocator.reverse(coordinates)\n",
    "\n",
    "    address = location.raw[\"address\"]\n",
    "\n",
    "    city = address.get(\"city\", \"\")\n",
    "    state = address.get(\"state\", \"\")\n",
    "    country = address.get(\"country\", \"\")\n",
    "\n",
    "    print(city, state, country)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 获取IP地址\n",
    "import socket\n",
    "\n",
    "hostname = socket.gethostname()\n",
    "ip = socket.gethostbyname(hostname)\n",
    "print(hostname, \": \", ip)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 日期显示\n",
    "import datetime as dt\n",
    "\n",
    "curdate = dt.datetime.now()\n",
    "print(curdate)\n",
    "\n",
    "# %A 星期全名\n",
    "# %B 月份全名\n",
    "# %d 一个月的几号\n",
    "# %Y 年份\n",
    "\n",
    "print(f\"{curdate:%Y-%B-%d %A}\")\n",
    "print(f\"{curdate:%Y-%b-%d %a}\")\n",
    "print(curdate.month)\n",
    "print(curdate.hour)\n",
    "print(curdate.minute)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 下面代码输出什么？\n",
    "l = [\"abcd\", 786, 2.23, \"john\", 70.2]\n",
    "print(l * 2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 下面代码的输出\n",
    "tuple = (\"abcd\", 786, 2.23, \"john\", 70.2)\n",
    "print(tuple[0])\n",
    "print(tuple[1:3])\n",
    "print(tuple[2:])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 下面代码输出什么？\n",
    "tuple = (123, \"Jerry\")\n",
    "print(tuple * 2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 下面代码的输出\n",
    "tuple1 = (\"abcd\", 786, 2.23, \"john\", 70.2)\n",
    "tuple2 = (123, \"Jerry\")\n",
    "print(tuple1 + tuple2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 字典的定义方法及键值对\n",
    "dict = {}\n",
    "dict[\"name\"] = \"Jinzd\"\n",
    "dict[\"gender\"] = \"Male\"\n",
    "dict[\"birth_date\"] = \"1968-08-20\"\n",
    "\n",
    "print(dict)\n",
    "\n",
    "dict2 = {\n",
    "    \"title\": \"Python Programming\",\n",
    "    \"author\": \"Joseph Silverman\",\n",
    "    \"price\": 9.99\n",
    "}\n",
    "\n",
    "print(dict2.keys())\n",
    "print(dict2.values())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 字符串转换为整数\n",
    "a = \"123\"\n",
    "b = int(a)\n",
    "print(type(a), type(b))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 判断下面代码的输出\n",
    "c = \"123b\"\n",
    "d = int(c)\n",
    "print(d)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 将字符串转换为浮点数\n",
    "a = \"1234.56\"\n",
    "b = float(a)\n",
    "print(type(a), type(b))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 把对象转换为正则表达式\n",
    "employee = {\n",
    "    \"name\": \"WuJun\",\n",
    "    \"gender\": \"male\",\n",
    "    \"basesalary\": 6000,\n",
    "    \"hourlyrate\": 40,\n",
    "    \"extrahours\": 10\n",
    "}\n",
    "\n",
    "r = repr(employee)\n",
    "print(r)\n",
    "print(\"r\", type(r))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 把字符串转换为对象\n",
    "s = \"{'name': 'WuJun', 'gender': 'male', 'basesalary': 6000, 'hourlyrate': 40, 'extrahours': 10}\"\n",
    "print(type(s))\n",
    "o = eval(s)\n",
    "print(type(o))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 把字符串转换为元组\n",
    "test = \"1, -5, 4, 6, 7\"\n",
    "  \n",
    "res = eval(test)\n",
    "print(type(res))\n",
    "print(str(res))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = 12\n",
    "b = 17\n",
    "print(f\"{a} + {b} = {a + b}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = 123\n",
    "b = \"456\"\n",
    "print(a + int(b))\n",
    "print(str(a) + b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "s = \"this is demo\"\n",
    "print(s[0])\n",
    "print(s[1])\n",
    "print(s[-1])\n",
    "s[-2]\n",
    "s[3:]\n",
    "s[1:-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = 3\n",
    "b = 3\n",
    "a < b\n",
    "\n",
    "a = \"Ade\"\n",
    "b = \"cde\"\n",
    "\n",
    "a == b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "temperature = 10\n",
    "\n",
    "if temperature > 30:\n",
    "    print(\"有点热\")\n",
    "    print(\"多喝水\")\n",
    "    print(\"sss\")\n",
    "elif temperature > 20:\n",
    "    print(\"天气真好\")\n",
    "else:\n",
    "    print(\"有点凉了\")\n",
    "\n",
    "print(\"done\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "age = 17\n",
    "\n",
    "if age >= 18:\n",
    "    message = \"合格\"\n",
    "else:\n",
    "    message = \"不合格\"\n",
    "\n",
    "print(message)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "不合格\n"
     ]
    }
   ],
   "source": [
    "age = 16\n",
    "\n",
    "message = \"合格\" if age >= 18 else \"不合格\"\n",
    "\n",
    "print(message)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "不合格\n"
     ]
    }
   ],
   "source": [
    "high_income = False\n",
    "good_credit = True\n",
    "student = False\n",
    "\n",
    "if True and False:\n",
    "    print(\"合格\")\n",
    "else:\n",
    "    print(\"不合格\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n"
     ]
    }
   ],
   "source": [
    "import random\n",
    "\n",
    "for number in [random.randrange(1, 10, 2)]:\n",
    "    print(number)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3]\n"
     ]
    }
   ],
   "source": [
    "a = [1, 2, 3]\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "l = [1, 3, 5, 7, 9]\n",
    "\n",
    "for num in l:\n",
    "    print(num)\n",
    "    \n",
    "    if num == 5:\n",
    "        print(f\"num = {num}\")\n",
    "        continue\n",
    "    \n",
    "    print(\"hello\")\n",
    "else:\n",
    "    print(\"finished\")\n",
    "    print(\"xxx\")\n",
    "    \n",
    "print(\"done\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 114,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "x is 0, y is 0\n",
      "x is 1, y is 0\n",
      "x is 2, y is 0\n",
      "x is 3, y is 0\n",
      "x is 4, y is 0\n"
     ]
    }
   ],
   "source": [
    "for x in range(5):\n",
    "    for y in range(3):\n",
    "        print(f\"x is {x}, y is {y}\")\n",
    "        if x == 2 and y == 1:\n",
    "            print(\"hello\")\n",
    "            continue\n",
    "        break\n",
    "        \n",
    "        \n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "dict = {\n",
    "    \"name\": \"Jerry\",\n",
    "    \"gender\": \"male\",\n",
    "    \"age\": 18\n",
    "}\n",
    "\n",
    "\n",
    "for item in dict.keys():\n",
    "    print(item)\n",
    "    \n",
    "for item in dict.values():\n",
    "    print(item)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "number = 100\n",
    "\n",
    "while number > 0:\n",
    "    print(number)\n",
    "    number //= 2\n",
    "    # number = number // 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 124,
   "metadata": {},
   "outputs": [
    {
     "ename": "KeyboardInterrupt",
     "evalue": "Interrupted by user",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mKeyboardInterrupt\u001b[0m                         Traceback (most recent call last)",
      "\u001b[1;32m~\\AppData\\Local\\Temp/ipykernel_12776/3860519021.py\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      2\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      3\u001b[0m \u001b[1;32mwhile\u001b[0m \u001b[0mcommand\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mlower\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;33m!=\u001b[0m \u001b[1;34m\"quit\"\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 4\u001b[1;33m     \u001b[0mcommand\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0minput\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"> \"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      5\u001b[0m     \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"echo\"\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mcommand\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32m~\\AppData\\Local\\Programs\\Python\\Python38\\lib\\site-packages\\ipykernel\\kernelbase.py\u001b[0m in \u001b[0;36mraw_input\u001b[1;34m(self, prompt)\u001b[0m\n\u001b[0;32m   1004\u001b[0m                 \u001b[1;34m\"raw_input was called, but this frontend does not support input requests.\"\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   1005\u001b[0m             )\n\u001b[1;32m-> 1006\u001b[1;33m         return self._input_request(\n\u001b[0m\u001b[0;32m   1007\u001b[0m             \u001b[0mstr\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mprompt\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   1008\u001b[0m             \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_parent_ident\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m\"shell\"\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32m~\\AppData\\Local\\Programs\\Python\\Python38\\lib\\site-packages\\ipykernel\\kernelbase.py\u001b[0m in \u001b[0;36m_input_request\u001b[1;34m(self, prompt, ident, parent, password)\u001b[0m\n\u001b[0;32m   1049\u001b[0m             \u001b[1;32mexcept\u001b[0m \u001b[0mKeyboardInterrupt\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   1050\u001b[0m                 \u001b[1;31m# re-raise KeyboardInterrupt, to truncate traceback\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 1051\u001b[1;33m                 \u001b[1;32mraise\u001b[0m \u001b[0mKeyboardInterrupt\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"Interrupted by user\"\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;32mfrom\u001b[0m \u001b[1;32mNone\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m   1052\u001b[0m             \u001b[1;32mexcept\u001b[0m \u001b[0mException\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   1053\u001b[0m                 \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mlog\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mwarning\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"Invalid Message:\"\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mexc_info\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;32mTrue\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mKeyboardInterrupt\u001b[0m: Interrupted by user"
     ]
    }
   ],
   "source": [
    "command = \"\"\n",
    "\n",
    "while command.lower() != \"quit\":\n",
    "    command = input(\"> \")\n",
    "    print(\"echo\", command)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 130,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hi there\n",
      "Welcome aboard\n",
      "dsdfs\n"
     ]
    }
   ],
   "source": [
    "def greet(x):\n",
    "    print(\"Hi there\")\n",
    "    print(\"Welcome aboard\")\n",
    "    print(x)\n",
    "\n",
    "def greetx(x):\n",
    "    print(\"Hi there\")\n",
    "    print(\"Welcome aboard\")\n",
    "    print(x)\n",
    "\n",
    "\n",
    "# greet(\"Hello\")\n",
    "greetx(\"dsdfs\")"
   ]
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "ee5cc6fef2d70a7e71ee3826687cbd150f18158e0b1eef11d4f4f92bb920e304"
  },
  "kernelspec": {
   "display_name": "Python 3.9.2 64-bit",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.6"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
